
require 'parse/command_parse'
require 'parse/prefix_parse'

#From the RFC:

#Each IRC message may consist of up to three main parts: 
#	-the prefix (optional), 
#	-the command, 
#	-and the command parameters 
#		(of which there may be up to 15). 
# All parameters are separated by one (or more) ASCII space character(s)

#Prefix details
#	-Single leading ASCII colon character (':', 0x3b), 
#		which must be the first character of the message itself.
#	-There must be no gap (whitespace) between the colon and the prefix.
#		If the prefix is missing from the message, it is assumed to have originated 
#		from the connection from which it was received.
#	Clients should not use prefix when sending a message from themselves; 
#		if they use a prefix, the only valid prefix is the registered nickname associated with the client.

#Command details
#	The command must either be a valid IRC command or a three (3) digit number represented in ASCII text.
#	
#	See command_parse for command syntax.


#Message details
#	-IRC messages are always lines of characters terminated with a Carriage Return - Line Feed(\r\n) pair
#
#	-These messages shall not exceed 512 characters in length, counting all characters including the trailing CR-LF.
#		Thus, there are 510 characters maximum allowed for the command and its parameters.

def parse_signal(message)
	split_message = message.split(" ")
	
	# If the message starts with a colon, it's got a prefix
	if split_message[0] =~ /^:/ and message.index(" ")
		prefixed = true
		# The prefix is in between the colon and the first space
		prefix = message[1...message.index(" ")]
		prefix_info = parse_prefix(prefix)
	end
	
	# if the command has a prefix, then the rest of the message is in split_message[1] - else, in 0
	# The command is everything except the last two characters, which are CR-LF
	if prefixed
		command = message.sub(":" + prefix + " ", "").strip
	else
		command = message.strip
	end
	
	command_info = parse_command(command)
	result = {}
	command_info.each do |key, value|
		result[key] = value
	end
	if prefix_info != nil
		prefix_info.each do |key, value|
			result[key] = value
		end
	end
	return result
end

#p parse_signal ":Angel!Heaven@Hostname-blarg.foo.bar PRIVMSG Wiz :Hello are you receiving this message ?\r\n"

#p parse_signal "PING  gfhiy23423"
